<?php
namespace app\ClubAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class LessonsController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('clubid'))
        {
            $this->redirect('index/index/index');
        }
    }
    public function index()
    {
        // 查询状态为1的用户数据 并且每页显示5条数据
         $list = Db::name('lessons')->where('cid',Session::get('clubid'))->paginate(5);
        // 把分页数据赋值给模板变量list
       $this->assign('list', $list);
        // 渲染模板输出
          return $this->fetch('lessons');
        
    }
    public function add()
    {
         $res=[
                'lname'=>input('lname'),
                'lintro'=>input('lintro'),
                'lprice'=>input('lprice'),
                'cid'=>Session::get('clubid'),

            ];
            $data=Db::name('lessons')
                ->insert($res);
            if ($data) {
                $this->redirect('index');
            } else {
                $this->error('添加失败', 'admin/Work/work');
            }
    }
    public  function delete($lid){
        
       if( db("lessons")->delete($lid)){
              $this->success("删除成功");
       }else{
           $this->error("删除失败");
       }

    }
     public function edit(){
      $lid=input('lid');
      $list=Db::name('lessons')->where('lid',$lid)->find();
      echo(json_encode($list)) ;

    }

    public function update(){
      //处理上传
       $lid=input('lid');
        //接受表单数据存到data
        $data=[
            'lname'=>input('lname'),
            'lintro' =>input('lintro'),// input(param.gender);
            'lprice' =>input('lprice'),
        ]; 

       if( Db::name('lessons')->where('lid',$lid)->update($data)){
          $this->success('更新课程成功！');

        }
        else
          $this->error('更新课程失败');

    }
      public function search(){
        $a=input('content');
     
        $sql=Db::table('lessons')
        ->where('lname','like',$a)
        ->paginate(5);
        //dump($sql);

     $this->assign('list',$sql);
     return $this->fetch('lessons');
    }
}